/*
* Copyright (c) 2014 Qunar.com. All Rights Reserved.
*/
package net.oschina.bilbodai.common.beanutil.property.asm.lexer.code;

import net.oschina.bilbodai.common.beanutil.property.asm.lexer.code.mp.MapKeyTypeMapping;
import net.oschina.bilbodai.common.beanutil.property.asm.lexer.code.mp.MapKeyType;
import net.oschina.bilbodai.common.beanutil.property.asm.lexer.code.mp.MapKeyTypes;
import org.objectweb.asm.MethodVisitor;

import java.util.LinkedList;

import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;

/**
 * Map默认的键类型为String，如果一个Map的键类型特殊，可以通过方式指定，比如：
 * {@code <int>age} 或者 {@code age<int>} 说明age是一个字符串类型的键
 * 详细请参考{@link MapKeyTypeMapping}
 *
 * @author qt-maodai  Date: 15-8-8 Time: 下午4:01
 * @version $Id$
 */
public class MapCodeApplier extends BaseCodeApplier {

    private final Class<?> valueType;

    public MapCodeApplier(CodeApply codeApply, Class<?> valueType) {
        super(codeApply);
        this.valueType = valueType;
    }

    @Override
    protected CodeApplyResult applyCode(MethodVisitor mv, CodeApply codeApply) throws Exception {
        MapKeyType type = MapKeyTypes.getType(codeApply.getMapKeyType());
        type.apply(mv, codeApply.getMapKey());
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        return new CodeApplyResult(valueType, Object.class, new LinkedList<Class<?>>());
    }

}